package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;

@Service
public class DishServicelmpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDIsh;
    /**
     * 修改菜品
     * @param dishDTO
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void modifydish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);
        //将原来的口味删除
        dishFlavorMapper.DelectByDishId(dishDTO.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();//判断口味是否为空
        if (flavors != null && flavors.size() > 0) {
            dishFlavorMapper.InsertByDishId(flavors);
            //插入新口味
        }
    }

    /**
     * 删除菜品
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByid(List<Long> ids) {
        // 在售状态下，不可删除菜品;   被套餐关联的菜品不能删除
        ids.forEach(id -> {
            Dish dish = dishMapper.selectById(id);
            if (dish.getStatus()== StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        ids.forEach(id -> {
            List<Long> longs = setmealDIsh.selectBydishId(id);
            if (longs != null && longs.size() > 0) {
                //关联了套餐
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        });
        dishMapper.deleteByid(ids);

    }

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Override
    public void sava(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setStatus(StatusConstant.DISABLE);
        dishMapper.Insert(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();//获取口味
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(flavor -> {
                flavor.setDishId(dish.getId());
            });
            dishFlavorMapper.InsertByDishId(flavors);
        }


    }

    /**
     * 根据id来查找信息
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public DishVO selectDishById(Long id) {
        DishVO dishVO = dishMapper.selectByIdVO(id);
       List<DishFlavor> dishFlavors =dishFlavorMapper.selectBydishId(id);
       dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    /**
     * 根据分类id来查找
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> listDish(Integer categoryId) {
       List<Dish> listDish = dishMapper.listDish(categoryId);
        return listDish;
    }

    /**
     * 分页查询
     * @param queryDTO
     * @return
     */
    @Override
    public PageResult pagelist(DishPageQueryDTO queryDTO) {
        //设置分页参数
        PageHelper.startPage(queryDTO.getPage(),queryDTO.getPageSize());
       List<DishVO>list =dishMapper.pagelist(queryDTO);
        Page<DishVO> result=(Page<DishVO>)list;
        return new PageResult(result.getTotal(),result.getResult());
    }

    /**
     * 修改状态
     *
     * @param status
     * @param id
     */
    @Override
    public void modifyStatus(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.update(dish);
    }

    @Override
    public List<DishVO> selectByCategoryId(Long categoryId) {
       List<DishVO> list =dishMapper.SelectBycategoryId(categoryId);

        for (DishVO dish : list) {
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectBydishId(dish.getId());
            if (dishFlavors != null && dishFlavors.size() > 0) {
                dish.setFlavors(dishFlavors);
            }
        }
        return list;
    }


}
